using System;
using System.Collections.Generic;
using System.Linq;
using LF.Application.Extend;
using LF.Domain.Entities;
using LF.Domain.Services;
using LF.Shared.Dtos.Input;
using LF.Shared.Dtos.Out;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.ObjectMapping;

namespace LF.Application.Services.Iml
{
    [Authorize]
    [ApiExplorerSettings(GroupName = SwaggerGroup.Sys)]
    public class ParamConfigService : BaseService, IParamConfigService
    {
        private readonly IParamConfigManagerService _paramConfigManager;

        public ParamConfigService(
            IObjectMapper<LFModule> mapper,
            IGlobalManagerService globalManager,
            IParamConfigManagerService paramConfigManager) : base(mapper, globalManager)
        {
            _paramConfigManager = paramConfigManager;
        }
        /// <summary>
        /// 获取参数配置列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [LFAuthorize("获取参数配置列表", "system:param-config:list")]
        public Result<ParamConfigSearchOut> List(ParamConfigSearch search)
        {
            var list = _paramConfigManager.All(search.Index, search.PageSize, search.Name, search.Key);
            var paramConfigs = _mapper.Map<List<ParamConfig>, List<ParamConfigOut>>(list);
            var count = _paramConfigManager.Count(search.Name, search.Key);
            
            return Ok(new ParamConfigSearchOut { Items = paramConfigs, TotalCount = count });
        }
        /// <summary>
        /// 获取参数配置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [LFAuthorize("获取参数配置信息", "system:param-config:read")]
        public Result<ParamConfigOut> Info(Guid id)
        {
            var paramConfig = _paramConfigManager.Get(id);
            if (paramConfig == null)
            {
                return Error<ParamConfigOut>("参数配置不存在!");
            }
            
            var result = _mapper.Map<ParamConfig, ParamConfigOut>(paramConfig);
            return Ok(result);
        }
        /// <summary>
        /// 创建参数配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [LFAuthorize("创建参数配置", "system:param-config:create")]
        public Result Create(ParamConfigInput input)
        {
            // 检查键名是否已存在
            var existConfig = _paramConfigManager.GetByKey(input.Key);
            if (existConfig != null)
            {
                return Error("参数键名已存在!");
            }

            var paramConfig = new ParamConfig
            {
                Id = Guid.NewGuid(),
                Name = input.Name,
                Key = input.Key,
                Value = input.Value,
                Remark = input.Remark,
                CreatTime = DateTime.Now,
                UpdateTime = null,
                IsDelete = false
            };
            
            bool result = _paramConfigManager.Insert(paramConfig);
            return result ? Ok() : Error("创建失败!");
        }
        /// <summary>
        /// 更新参数配置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [LFAuthorize("更新参数配置", "system:param-config:update")]
        public Result Update(Guid id, ParamConfigInput input)
        {
            var paramConfig = _paramConfigManager.Get(id);
            if (paramConfig == null)
            {
                return Error("参数配置不存在!");
            }
            
            // 检查键名是否被其他配置占用
            var existConfig = _paramConfigManager.GetByKey(input.Key);
            if (existConfig != null && existConfig.Id != id)
            {
                return Error("参数键名已被其他配置使用!");
            }
            
            paramConfig.Name = input.Name;
            paramConfig.Key = input.Key;
            paramConfig.Value = input.Value;
            paramConfig.Remark = input.Remark;
            bool result = _paramConfigManager.Update(paramConfig);
            return result ? Ok() : Error("更新失败!");
        }
        /// <summary>
        /// 删除参数配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [LFAuthorize("删除参数配置", "system:param-config:delete")]
        public Result Delete(Guid id)
        {
            var paramConfig = _paramConfigManager.Get(id);
            if (paramConfig == null)
            {
                return Error("参数配置不存在!");
            }
            
            bool result = _paramConfigManager.Delete(id);
            return result ? Ok() : Error("删除失败!");
        }

        public Result<ParamConfigOut> GetByKey(string key)
        {
            var paramConfig = _paramConfigManager.GetByKey(key);
            if (paramConfig == null)
            {
                return Error<ParamConfigOut>("参数配置不存在!");
            }
            
            var result = _mapper.Map<ParamConfig, ParamConfigOut>(paramConfig);
            return Ok(result);
        }
    }
} 